package com.quanyan.travel.service.impl;

import com.google.common.collect.Lists;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.travel.api.resp.RespTravelIndex;
import com.quanyan.travel.component.cache.MyRedisTemplate;
import com.quanyan.travel.component.cache.TravelRedisConstants;
import com.quanyan.travel.constants.OutdoorConstants;
import com.quanyan.travel.entity.*;
import com.quanyan.travel.entity.vo.Banner;
import com.quanyan.travel.entity.vo.Category;
import com.quanyan.travel.entity.vo.Destination;
import com.quanyan.travel.mapper.*;
import com.quanyan.travel.service.TravelCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 订单Service
 */


@Service("travelCacheService")
public class TravelCacheServiceImpl implements TravelCacheService {

    private static Logger logger = LoggerFactory.getLogger(TravelCacheServiceImpl.class);

    @Autowired
    TbIndexBannerMapper indexBannerMapper;

    @Autowired
    MyRedisTemplate redisTemplate;

    @Autowired
    TbOutdoorCategoryMapper outdoorCategoryMapper;

    @Autowired
    TbOutdoorDestinationMapper outdoorDestinationMapper;

    @Autowired
    TbOutdoorActivityMapper outdoorActivityMapper;

    @Autowired
    TbCityMapper cityMapper;

    @Autowired
    TbOutdoorActivityDestinationMapper outdoorActivityDestinationMapper;

    @Autowired
    VTravelUserDefinedMapper vTravelUserDefinedMapper;

    /**
     * 默认存放时间一周
     *
     * */
    private static final int DEFAULT_CACHE_SEC = 60 * 1 * 60 * 24 * 7;





    @Override
    public void updateBanner() {
        RedisKey rk = new RedisKey(RedisConstants.OUTDOOR_FAMILY_KEY, OutdoorConstants.INDEX_CACHE_KEY);
        RespTravelIndex respTravelIndex = new RespTravelIndex();
        logger.info("indexProperties查询缓存为空或者数据不合法，重新查询数据库！");
        TbIndexBannerExample indexBannerExample = new TbIndexBannerExample();
        TbIndexBannerExample.Criteria indexBannerCriteria = indexBannerExample.createCriteria();
        indexBannerCriteria.andTypeEqualTo((byte) 1);
        indexBannerCriteria.andIsDeleteEqualTo((byte) 0);
        List<TbIndexBanner> banners = indexBannerMapper.selectByExample(indexBannerExample);
        List<Banner> bannerList = Lists.newArrayList();
        if (banners != null && banners.size() > 0) {
            for (TbIndexBanner tbIndexBanner : banners) {
                Banner banner = new Banner();
                BeanUtils.copyProperties(tbIndexBanner, banner);
                banner.setActivityId(tbIndexBanner.getLinkId());
                banner.setIndex(tbIndexBanner.getSequence());
                bannerList.add(banner);
            }
        }
        respTravelIndex.setBanners(bannerList);

        TbOutdoorCategoryExample outdoorCategoryExample = new TbOutdoorCategoryExample();
        TbOutdoorCategoryExample.Criteria OutdoorCategoryCriteria = outdoorCategoryExample.createCriteria();
        OutdoorCategoryCriteria.andIsDeleteEqualTo(OutdoorConstants.IS_DELETE_FALSE);
        outdoorCategoryExample.setOrderByClause("priority DESC");
        List<TbOutdoorCategory> categories = outdoorCategoryMapper.selectByExample(outdoorCategoryExample);
        List<Category> categoryList = Lists.newArrayList();
        Category allType = new Category();
        allType.setCategoryId(0);
        allType.setExtendName("全部玩法");
        allType.setName("全部玩法");
        allType.setCategoryId(0);
        categoryList.add(allType);
        if (categories != null && categories.size() > 0) {
            for (TbOutdoorCategory tbOutdoorCategory : categories) {
                Category category = new Category();
                category.setCategoryId(tbOutdoorCategory.getId());
                BeanUtils.copyProperties(tbOutdoorCategory, category);
                categoryList.add(category);
            }
            respTravelIndex.setCategories(categoryList);
        }
        TbCityExample tbCityExample = new TbCityExample();
        TbCityExample.Criteria cityCriteria = tbCityExample.createCriteria();
        cityCriteria.andIsEnableEqualTo(1);
        cityCriteria.andIsActiveEqualTo(1);
        tbCityExample.setOrderByClause("priority DESC");
        List<TbCity> tbCities = cityMapper.selectByExample(tbCityExample);
        List<Destination> destinationList = Lists.newArrayList();
        Destination allDestination = new Destination();
        allDestination.setCityId(0);
        allDestination.setName("全部城市");
        allDestination.setAddress("全部城市");
        allDestination.setId(0);
        destinationList.add(allDestination);
        for(TbCity city : tbCities){
            Destination destination = new Destination();
            BeanUtils.copyProperties(city,destination);
            destination.setCityId(city.getId());
            destinationList.add(destination);
        }
        respTravelIndex.setDestinations(destinationList);
        redisTemplate.set(rk, respTravelIndex);
    }



    @Override
    public void initOrderRemindToCache(int uid, String orderNo) {
        RedisKey rk = new RedisKey(RedisConstants.OUTDOOR_FAMILY_KEY, TravelRedisConstants.getUserRemindOrderKey(uid));
        redisTemplate.sadd(rk, orderNo);
        redisTemplate.expire(rk, this.DEFAULT_CACHE_SEC);
    }

    @Override
    public void updateOrderRemindToCache(int uid, String orderNo) {
        RedisKey rk = new RedisKey(RedisConstants.OUTDOOR_FAMILY_KEY, TravelRedisConstants.getUserRemindOrderKey(uid));
        redisTemplate.srem(rk, orderNo);
    }

    @Override
    public Set<String> getOrderRemindFromCache(int uid) {
        RedisKey rk = new RedisKey(RedisConstants.OUTDOOR_FAMILY_KEY, TravelRedisConstants.getUserRemindOrderKey(uid));
        return redisTemplate.smembers(rk);
    }
}
